package org.jeecg.modules.product.risk.service.impl;

import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.product.risk.entity.TbBaseControlMeasures;
import org.jeecg.modules.product.risk.entity.TbBaseMeasuresTask;
import org.jeecg.modules.product.risk.mapper.TbBaseControlMeasuresMapper;
import org.jeecg.modules.product.risk.mapper.TbBaseMeasuresTaskMapper;
import org.jeecg.modules.product.risk.service.ITbBaseControlMeasuresService;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 管控措施信息
 * @Author: jeecg-boot
 * @Date:   2024-03-02
 * @Version: V1.0
 */
@Service
public class TbBaseControlMeasuresServiceImpl extends ServiceImpl<TbBaseControlMeasuresMapper, TbBaseControlMeasures> implements ITbBaseControlMeasuresService {
	
	@Autowired
	private TbBaseControlMeasuresMapper tbBaseControlMeasuresMapper;
	@Autowired
	private TbBaseMeasuresTaskMapper tbBaseMeasuresTaskMapper;
	
	@Override
	public List<TbBaseControlMeasures> selectByMainId(String mainId) {
		return tbBaseControlMeasuresMapper.selectByMainId(mainId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(TbBaseControlMeasures tbBaseControlMeasures, List<TbBaseMeasuresTask> tbBaseMeasuresTaskList) {
		tbBaseControlMeasures.setId(UUIDGenerator.generate());
		tbBaseControlMeasuresMapper.insert(tbBaseControlMeasures);
		if(tbBaseMeasuresTaskList!=null && tbBaseMeasuresTaskList.size()>0) {
			for(TbBaseMeasuresTask entity:tbBaseMeasuresTaskList) {
				//外键设置
				entity.setRiskMeasureId(tbBaseControlMeasures.getId());
				tbBaseMeasuresTaskMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(TbBaseControlMeasures tbBaseControlMeasures,List<TbBaseMeasuresTask> tbBaseMeasuresTaskList) {
		tbBaseControlMeasuresMapper.updateById(tbBaseControlMeasures);

		//1.先删除子表数据
		//tbBaseMeasuresTaskMapper.deleteByMainId(tbBaseControlMeasures.getId());

		//2.子表数据重新插入
		if(tbBaseMeasuresTaskList!=null && tbBaseMeasuresTaskList.size()>0) {
			for(TbBaseMeasuresTask entity:tbBaseMeasuresTaskList) {
				if(null != entity.getId()){
					tbBaseMeasuresTaskMapper.updateById(entity);
				}else{
					//外键设置
					entity.setRiskMeasureId(tbBaseControlMeasures.getId());
					entity.setId(UUIDGenerator.generate());
					tbBaseMeasuresTaskMapper.insert(entity);
				}
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		tbBaseMeasuresTaskMapper.deleteByMainId(id);
		tbBaseControlMeasuresMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			tbBaseMeasuresTaskMapper.deleteByMainId(id.toString());
			tbBaseControlMeasuresMapper.deleteById(id);
		}
	}


}
